{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "图片的通道： ('R', 'G', 'B')\n",
      "图片的通道数： 3\n",
      "<PIL.Image.Image image mode=RGB size=28x28 at 0x7F7703DE4780>\n",
      "<PIL.Image.Image image mode=L size=28x28 at 0x7F7703DE46D8>\n",
      "(28, 28)\n",
      "784\n",
      "<class 'numpy.ndarray'>\n",
      "uint8\n",
      "[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.\n",
      "  255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.\n",
      "  255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.\n",
      "  255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0. 255. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0. 255. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0. 255. 255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255.\n",
      "  255.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.\n",
      "  255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.   0.   0.   0.]\n",
      " [  0. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.\n",
      "  255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255. 255.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0. 255. 255.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.\n",
      "    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]]\n",
      "float32\n",
      "transfer binfile1 finish!!!\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "#方法1：使用img_kreas.tofile保存图片到bin文件\n",
    "import struct\n",
    "from keras.preprocessing import image\n",
    "import cv2\n",
    "from keras.models import load_model\n",
    "import numpy as np\n",
    "import time\n",
    "img_path = \"./4-1.png\" \n",
    "#kreas使用PIL加载RGB图片 ,image.load_img()只是加载了一个文件，没有形成numpy数组!!!\n",
    "img = image.load_img(img_path,target_size=(28,28))       \n",
    "print(\"图片的通道：\",img.getbands())\n",
    "print(\"图片的通道数：\",len(img.getbands()))\n",
    "print(img)                      #mode=RGB\n",
    "img =img.convert('L')           #图片转为灰度值\n",
    "print(img)                      #mode=L\n",
    "#img_kreas= image.img_to_array(img)          #将图片中的像素值转化为浮点型数组，每一个像素值为4Byte float\n",
    "img_kreas= np.asarray(img)                   #将图片中的像素值转化为整型数组,   每一个像素值为1Byte float   \n",
    "#print(img_kreas)\n",
    "print(img_kreas.shape)          #查看数组的形状  28x28x1\n",
    "print(img_kreas.size)           #查看数组的元素个数\n",
    "print(type(img_kreas))           #查看img_kreas的数据类型---数组\n",
    "\n",
    "print(img_kreas.dtype)           #查看数组的类型\n",
    "img_kreas=img_kreas.astype('float32')      #将数组的数据类型改为int32\n",
    "#img_kreas=img_kreas.transpose(1,0,2)     #对数据进行转置成按行排列的形式\n",
    "print(img_kreas)\n",
    "print(img_kreas.dtype)           #查看数组中数据的数据类型\n",
    "img_kreas.tofile(\"./input_float32.bin\")     #将数组保存到二进制文件\n",
    "print(\"transfer binfile1 finish!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "图片的通道： ('L',)\n",
      "图片的通道数： 1\n",
      "28 28\n",
      "<class 'int'>\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 255.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 255.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 255.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 255.0 255.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
      "\n",
      "tansfer binfile2 finish!!!\n"
     ]
    }
   ],
   "source": [
    "#方法2：使用struct模块，在文件中添加图片的宽高通道数\n",
    "#x = image.img_to_array(img)\n",
    "#print(x/255.0)\n",
    "w,h= img.size        #获取图片的宽、高、通道数\n",
    "channels=len(img.getbands())\n",
    "print(\"图片的通道：\",img.getbands())\n",
    "print(\"图片的通道数：\",channels)\n",
    "print(w,h)\n",
    "f = open('input_structfloat32'+'.bin','wb')     #打开目的文件\n",
    "print(type(w))\n",
    "'''\n",
    "有的时候需要用python处理二进制数据，\n",
    "比如，存取文件，socket操作时.这时候，可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体.\n",
    "'''\n",
    "f.write(struct.pack('i',int(w)))       #写入图片宽到目的文件中\n",
    "time.sleep(0.1)\n",
    "f.write(struct.pack('i',int(h)))       #写入图片高到目的文件中\n",
    "time.sleep(0.1)\n",
    "f.write(struct.pack('i',int(channels)))       #写入图片的通道数到目的文件中\n",
    "time.sleep(0.1)\n",
    "\n",
    "for i in range(h):                     #按行写入图片像素到目的文件中\n",
    "    for j in range(w):\n",
    "        pixel_r = float(img.getpixel((j,i)))    #每一个像素为4Byte  int型\n",
    "        print(pixel_r,end=' ')\n",
    "        f.write(struct.pack('f',pixel_r))\n",
    "        time.sleep(0.001)\n",
    "    print(\"\\n\")\n",
    "print(\"tansfer binfile2 finish!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
